<?php
namespace app\service;
use think\Db;
class couponService{
    //根据区域id查询商户信息
    public function propertyList($property_id)
    {
        return Db::name('property')->where('id','in',$property_id)->select();
    }
	//根据区域id查询商户信息
    public function communityList($community_id)
    {
        return Db::name('community')->where('id','in',$community_id)->select();
    }

    //通过商户id查询商户token
    public function property_token($id)
    {
        return Db::name('property')->where('id',$id)->value('accredit');
    }

    //通过商户id查询商户pid
    public function property_pid($id)
    {
        return Db::name('property')->where('id',$id)->value('agent_id');
    }

    //将数据存入数据库
    public function couponAdd($data)
    {
        return Db::name('coupon')->insert($data);
    }

    //通过停车场id查询支付宝停车场id
    public function ali_parkingId($id)
    {
        return Db::name('parkinglotinfo')->where('id',$id)->value('parking_id');
    }

    //根据商户id查询优惠券
    public function couponSelect($property_id,$where,$page)
    {
        return Db::name('coupon')
            ->alias('c')
            ->join('property p','c.property_id = p.id')
            ->field('c.*,p.merchant_name')
            ->where('property_id','in',$property_id)
            ->where($where)
            ->order('id desc')
            ->paginate(10, false, [
                'page'  =>  $page,
                'query' =>  $where
            ]);
    }
	
    //根据id查询优惠券信息
    public function couponData($id)
    {
        return Db::name('coupon')->where('id',$id)->find();
    }

    //修改数据库优惠券剩余数
    public function couponSurplus($id,$surplus)
    {
		if($surplus < 0){
			$surplus = 0;
		}
        return Db::name('coupon')->where('id',$id)->update(['surplus'=>$surplus]);
    }
	//--------------------------------------------固定优惠券-----------------------------------------------
    //根据商户id查询停车场信息
    public function parkingInfo($community_id)
    {
        return Db::name('parkinglotinfo')->where('community_id','in',$community_id)->select();
    }
    //添加固定优惠券
    public function fixCouponAdd($fixcoupon_data)
    {
        return Db::name('fixcoupon')->insert($fixcoupon_data);
    }
    //查询优惠券列表
    public function fixCouponList($out_parking_id)
    {
        return Db::name('fixcoupon')
            ->alias('f')
            ->join('parkinglotinfo p','f.out_parking_id = p.out_parking_id')
            ->field('f.*,p.parking_name')
            ->where('f.out_parking_id','in',$out_parking_id)
            ->order('id desc')
            ->paginate(15);
    }
    //根据id查询优惠券
    public function fixCouponInfo($id)
    {
        return Db::name('fixcoupon')
            ->alias('f')
            ->join('parkinglotinfo p','f.out_parking_id = p.out_parking_id')
            ->field('f.*,p.parking_name')
            ->where('f.id',$id)
            ->find();
    }
    //修改
    public function fixCouponEdit($data)
    {
        return Db::name('fixcoupon')->update($data);
    }
    //通过车牌号和停车场id查询订单
    public function fixOrderInfo($car_number,$out_parking_id)
    {
        return Db::name('car_order')->where('car_number',$car_number)->where('out_parking_id',$out_parking_id)->where('order_status','TRADE_SUCCESS')->find();
    }
    //通过停车场id查询停车场
    public function fixParkingInfo($out_parking_id)
    {
        return Db::name('parkinglotinfo')->where('out_parking_id',$out_parking_id)->find();
    }
    //修改订单状态
    public function orderEdit($data)
    {
        return Db::name('car_order')->update($data);
    }
    //查询固定优惠券使用次数
    public function fixCouponUsed($fixcoupon_id)
    {
        return Db::name('fixcoupondetail')->where("fixcoupon_id",$fixcoupon_id)->where('use_time','>',date('Y-m-d 00:00:00',time()))->where('use_time','<',date('Y-m-d 23:59:59',time()))->count();
    }
    //写入固定卷使用详情
    public function fixCouponDetail($fixCoupon)
    {
        return Db::name('fixcoupondetail')->insert($fixCoupon);
    }
    //固定卷使用详情
    public function fixCouponDetailList($where,$page,$out_parking_id)
    {
        return Db::name('fixcoupondetail')->where('out_parking_id','in',$out_parking_id)->where($where)->order('id desc')->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
    }
	
	//查询停车优惠券列表
    public function couponParkList($out_parking_id)
    {
        return Db::name('coupon_park')
            ->alias('f')
            ->join('parkinglotinfo p','f.out_parking_id = p.out_parking_id')
            ->field('f.*,p.parking_name')
            ->where('f.out_parking_id','in',$out_parking_id)
			->where('f.status','1')
            ->order('id desc')
            ->paginate(15);
    }
	public function couponParkDetailList($where,$page,$out_parking_id){
		return Db::name('couponinfo_park')->where('out_parking_id','in',$out_parking_id)->where($where)->order('id desc')->paginate(10, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
	}


	public function getOrderPay($info){
        $orderinfo["parkingNo"]=$info["parkingNo"];
        $orderinfo["parkingName"]=$info["parkingName"];
        $orderinfo["clientNo"]=$info["clientNo"];
        $orderinfo["outParkingOrderNo"]=$info["outParkingOrderNo"];
        $orderinfo["amountRec"]=$info["amountRec"];
        $orderinfo["amountDue"]=$info["amountDue"];
        $orderinfo["parkingStartTime"]=$info["parkingStartTime"];
        $orderinfo["user_id"]=$info["user_id"];
        $orderinfo["paytime"]=date("YmdHis");
        $orderinfo["carPlateNo"]=$info["carPlateNo"];
        $orderinfo["reqNo"]=date("YmdHis")."-".rand(100000,999999);
        $orderinfo["parkingTime"]=$info["parkingTime"];
        $orderinfo["tradeno"]=date("YmdHis").rand(100000,999999);
        $orderinfo["out_trade_no"]=$orderinfo["tradeno"];
        $orderinfo["outChannelNo"]=$info["outChannelNo"];
        $orderinfo["subject"]=$info["subject"];
        $orderinfo["payType"]=$info["payType"];
        $orderinfo["parkingEndTime"]=date("YmdHis");
        return $orderinfo;
    }
}
